//
//  NYEditViewModel.m
//  Nooryes
//
//  Created by baken on 2020/3/7.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "NYEditViewModel.h"
#import "NYMainModel.h"
#import "NYAccountManager.h"
#import "NYMainManager.h"
#import "NYLoginManager.h"
#import "AccountModel.h"
#import "NYAccountModel.h"
#import "NYRegionListModel.h"


@implementation NYEditViewModel

-(void)modifyPasswordWithCompletionHandle:(CompletionHandle)complete{
    [NYLoginManager modifyPwdWithPhoneNum:self.phone andPassword:self.password andVerifyCode:self.code andCompletionHandle:^(NYMainModel *model, NSError *error) {
        if (!error) {
            
        }
        complete(error);
    }];
}

-(void)modifyPhoneWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    [NYLoginManager resetPhoneWithUserID:uid andSessionID:sessionid andPhone:self.phone andCode:self.code andCompletionHandle:^(NYMainModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            [NYAccountManager getUserInfoWithUserID:uid andSessionID:sessionid andCompletionHandle:^(NYAccountModel * model, NSError * error) {
                          if (!error) {
                              myDelegate.account = model.data;
                          }
                          complete(error);
                      }];
                      
                      [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:self];
        }
        complete(error);
    }];
}


-(void)modifyUserInfoWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    [NYLoginManager modifyUserInfoWithUserID:uid andSessionID:sessionid andNickname:self.userName andNewID:self.userID andCity:self.userCity andGender:STR_FROM_INT(self.selectedSex) andBirthday:self.userBirthday andDesc:self.userDesc andImg:self.imgUrl andCompletionHandle:^(NYMainModel *model, NSError *error) {
        if (!error) {
            [NYAccountManager getUserInfoWithUserID:uid andSessionID:sessionid andCompletionHandle:^(NYAccountModel * model, NSError * error) {
                if (!error) {
                    myDelegate.account = model.data;
                }
                complete(error);
            }];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:self];
        }
    }];
}

-(void)modifyAvatarWithCompletionHandle:(CompletionHandle)complete{
    [NYMainManager modifyAvatarWithImage:self.image andCompletionHandle:^(NYMainModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            self.imgUrl = model.data;
        }
        complete(error);
    }];
}

-(void)getRegionWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    [NYLoginManager getRegionListWithUserID:uid andSessionID:sessionid andParentID:self.parentid andLevel:self.level andCompletionHandle:^(NYRegionListModel *model, NSError *error) {
    if (!error) {
        if ([self.parentid isEqualToString:@"0"]) {
            [self.provinceArray removeAllObjects];
            [self.provinceArray addObjectsFromArray:model.data];
        }
        if (![self.parentid isEqualToString:@"0"]) {
            [self.cityArray removeAllObjects];
            [self.cityArray addObjectsFromArray:model.data];
        }
      }
      complete(error);
    }];
}


-(NSMutableArray *)provinceArray{
    if (!_provinceArray) {
        _provinceArray  = [NSMutableArray array];
    }
    return _provinceArray;
}


-(NSMutableArray *)cityArray{
    if (!_cityArray) {
        _cityArray  = [NSMutableArray array];
    }
    return _cityArray;
}
@end
